/**
 * Date:2022年1月6日 上午10:15:18
 * Copyright (c) 2022, www.bwbroad.com All Rights Reserved
 */

package com.app.common.utils;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneOffset;

import org.apache.commons.beanutils.Converter;

public class DebeziumDateConvert implements Converter {
	/**
	 * @param type  Data type to which this value should be converted 将要被被转化到的类型
	 * @param value The input value to be converted 被转化的值
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public Object convert(Class type, Object value) {
//		System.out.println("type=" + type + "-> value:" + value + "【" + (null != value ? value.getClass() : "") + "】");
		if (value instanceof java.lang.Long ts) {
			// ts是距离1970-01-01的毫秒数
			LocalDateTime dt = LocalDateTime.ofInstant(Instant.ofEpochMilli(ts), ZoneOffset.UTC);
			// LocalDateTime dt = LocalDateTime.ofEpochSecond(ts / 1000, 0,
			// ZoneOffset.UTC);
			return Utils.toDate(dt);
		} else if (value instanceof java.lang.Integer day) {
			// day是距离1970-01-01的天数
			return Utils.toDate(LocalDate.ofEpochDay(day));
		} else if (value instanceof String str) {
			return Utils.toDateFromUTC(str);
		}
		return null;
	}

}